<?php

/**
 * Green Calc
 * Calc menu options (get calc from DB)
 * 2009-2014 All rights reserved
 * @link http://www.greenkey.ru
 * @license GNU/GPL v3
 */
// No direct access
defined('_JEXEC') or die;

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

/**
 * Calc Form Field class for the Calc component
 */
class JFormFieldCalc extends JFormFieldList {

    /**
     * The field type.
     * @var string
     */
    protected $type = 'Calc';

    /**
     * Method to get a list of calc for a list input (menu selection)
     *
     * @return      array           An array of JHtml options.
     */
    protected function getOptions() {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $query->select('id, caption');
        $query->from('#__calc');
        $query->where('state > 0');
        $db->setQuery((string) $query);
        $caps = $db->loadObjectList();
        
        $options = array();
        
        if ($caps) {
            foreach ($caps as $cap) {
                $options[] = JHtml::_('select.option', $cap->id, $cap->caption);
            }
        } else {
            $options[] = JHtml::_('select.option', 0, JText::_('COM_CALC_ERROR_MESSAGE_NOT_FOUND'));
        }
        
        $options = array_merge(parent::getOptions(), $options);
        return $options;
    }

}
